﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Search2 : System.Web.UI.Page
    {
        ShopingCart Cart = new ShopingCart();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadNews();
            }
        }
        public void loadNews()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["sid"]))
            {
                string type = Request.QueryString["sid"];
                if (type.Equals("sp"))
                {
                    rpt.Visible = true;
                    rptNews.Visible = false;
                    if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    {
                        DataTable dt = new cmsSanPhamBL().TimKiem(Request.QueryString["ID"].ToString());
                        rptSanPhamDataSource(dt);
                        lblInfo.Text = "Có " + dt.Rows.Count + " sản phẩm có tên <a href='/Search/" + Request.QueryString["ID"].ToString() + "/Tim-Kiem.aspx'><span style='color:red;font-weight:bold;'>" + Request.QueryString["ID"].ToString() + "</span></a>";
                        Page.Title = Request.QueryString["ID"].ToString() + " - cayxanhdangviet.com";
                    }
                }
                else if(type.Equals("tt"))
                 {
                     rpt.Visible = false;
                     rptNews.Visible = true;
                     if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                     {
                         DataTable dt = new cmsArticleBL().SearchByKeyword(Request.QueryString["ID"].ToString());
                         rptNewsDataSource(dt);
                         lblInfo.Text = "Có " + dt.Rows.Count + " tin tức được tìm thấy";
                         Page.Title = Request.QueryString["ID"].ToString() + " - cayxanhdangviet.com";
                     }
                }
            }
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        protected void rptNewsDataSource(DataTable dt)
        {
            CollectionPager1.MaxPages = 10000;

            CollectionPager1.PageSize = 24; // số items hiển thị trên một trang

            CollectionPager1.DataSource = dt.DefaultView;

            CollectionPager1.BindToControl = rptNews;
            rptNews.DataSource = CollectionPager1.DataSourcePaged;
            rptNews.DataBind();
        }
        protected void rptSanPhamDataSource(DataTable dt)
        {
            CollectionPager1.MaxPages = 10000;

            CollectionPager1.PageSize = 24; // số items hiển thị trên một trang

            CollectionPager1.DataSource = dt.DefaultView;

            CollectionPager1.BindToControl = rpt;
            rpt.DataSource = CollectionPager1.DataSourcePaged;
            rpt.DataBind();
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            rptNews.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (!string.IsNullOrEmpty(Request.QueryString["sid"]))
            {
                 string type = Request.QueryString["sid"];
                 if (type.Equals("sp"))
                 {
                     if (dt.Rows.Count > 0)
                     {
                         rpt.Visible = true;
                         rptNews.Visible = false;
                         rpt.DataSource = objPds;
                         rpt.DataBind();
                     }
                 }
                 if (type.Equals("tt"))
                 {
                     if (dt.Rows.Count > 0)
                     {
                         rpt.Visible = false;
                         rptNews.Visible = true;
                         rptNews.DataSource = objPds;
                         rptNews.DataBind();
                     }
                 }

               
            }
            
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            loadNews();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            loadNews();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
    }
}
